/**
 *  题目一：通讯录管理系统的设计与实现 
 设计要求：系统包括通讯者结点信息的插入、查询、删除、
 更新以及通讯录信息的输出等功能。 
  菜单内容： 
  1. 通讯录链表的建立 
  2. 通讯者信息的插入 
  3. 通讯者信息的查询 
  4. 通讯者信息的修改 
  5. 通讯者信息的删除 
  6. 通讯录链表的输出 
  7. 退出管理系统 
  请选择：1 – 7：
 */

#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;

typedef int ID;

void menu();

class Contact {
	private:
		struct man {
			string name;
			string phoneNumber;
			struct man *next;
		} *Head;
		int nCount;
		string tname, tphonenumber;

	public:
		enum Stat {SUCCESS, FAILURE, EMPTY, NOTEMPTY};
		struct man* Root; 
		// 构造
		Contact() {
			nCount = 0;
		}

		// 析构
		~Contact() {}

		// 拷贝构造
		//Contact(const Contact &pt) {
		//	man = pt.man;
		//}

		
		Stat Init() {
			// man* top = new man;
			// top->name = "sss";
			// top.phoneNumber = "1231";

			// Root = top;
			Root = new man;
			Root->next = NULL;

			// cout << Root->name << endl;

			cout << "初始化成功!!!" << endl;

			return SUCCESS;
		}
		
		// 个人信息
		man* manData() {
			man *one = new man;
			cout << "Input Name: ";
			cin >> one->name;
			cout << "Input phoneNumber: ";
			cin >> one->phoneNumber;
			one->next = NULL;
			// cout << "sss" << endl;
			//cout << Root->name << endl;

			return one;
		}

		// 插入
		Stat Insert() {
			man* one = manData();
			// cout << one->name << one->phoneNumber << endl;

			// 插入到结尾
			man *pre, *cur;
			for(cur=Root, pre=Root; cur->next!=NULL && one->name <= cur->name; 
				pre=cur, cur=cur->next)
					;
			// cout << Root << " " << pre << endl;
			// cout << Root->name << endl;
			one->next = pre->next;
			pre->next = one;

			++nCount;
			
			return SUCCESS;
		}

		// 
		Stat IsEmpty() {
			if(nCount == 0)
				return EMPTY;
			return NOTEMPTY;
		}
	
		Stat InputName() {
			cout << "Input Name: ";
			cin >> tname;
			// cout << name;

			return SUCCESS;
		}

		Stat InputPhoneNumber() {
			cout << "Input Phone Number: ";
			cin >> tphonenumber;
			// cout << *number;
			// cout << number;

			return SUCCESS;
		}

		man* Search_By_Name(string name) {
			man* begin = Root->next;
			for(; begin != NULL; begin=begin->next) {
				if(begin->name == name) {
					cout << begin->name << " " << begin->phoneNumber << endl;
					return begin;
				}
			}

			cout << "Contact has no member named " << name << " ." << endl;
			return NULL;
		}

		man* Search_By_Phone(string phoneNumber) {
			man* begin = Root->next;
			for(; begin != NULL; begin=begin->next) {
				if(begin->phoneNumber == phoneNumber) {
					cout << begin->name << " " << begin->phoneNumber << endl;
					return begin;
				}
			}

			cout << "Contact has no member phone number is " << phoneNumber << " ." << endl;
			return NULL;
		}

		Stat Search() {
			if(IsEmpty() == EMPTY) {
				cout << "Sorry, Contact is Empty." << endl;
				return FAILURE;
			}
	
			cout << "1、By Name" << endl;
			cout << "2、By Phone Number" << endl;
			cout << "Please Choose: " << endl;

			string choice;
			cin >> choice;
			while(choice[0]!='1' && choice[0]!='2') {
				cout << "Error.Try Again." << endl;
				cout << "Please Choose: " << endl;
				cin >> choice;
			}

			switch(choice[0]) {
				case '1':
					InputName();
					//cout << *tname << endl;
					Search_By_Name(tname);
					break;
				case '2':
					InputPhoneNumber();
					//string *tnumber = InputPhoneNumber();
					//cout << *tnumber << endl;
					Search_By_Phone(tphonenumber);
					break;
			}
			
			return SUCCESS;
		}

		Stat Modify() {
			if(IsEmpty() == EMPTY) {
				cout << "Sorry, Contact is Empty." << endl;
				return FAILURE;
			}
	
			cout << "1、By Name" << endl;
			cout << "2、By Phone Number" << endl;
			cout << "Please Choose: " << endl;

			string choice;
			cin >> choice;
			while(choice[0]!='1' && choice[0]!='2') {
				cout << "Error.Try Again." << endl;
				cout << "Please Choose: " << endl;
				cin >> choice;
			}

			man* temp;
			switch(choice[0]) {
				case '1':
					InputName();
					//cout << *tname << endl;
					temp = Search_By_Name(tname);
					if(temp != NULL) {
						cout << "Input new Name: ";
						cin >> temp->name;
						cout << "Input new Phone Number: ";
						cin >> temp->phoneNumber;
					}
					break;
				case '2':
					InputPhoneNumber();
					//string *tnumber = InputPhoneNumber();
					//cout << *tnumber << endl;
					temp = Search_By_Phone(tphonenumber);
					if(temp != NULL) {
						cout << "Input new Name: ";
						cin >> temp->name;
						cout << "Input new Phone Number: ";
						cin >> temp->phoneNumber;
					}
					break;
			}
			
			return SUCCESS;
		}


		Stat Delete_By_Name(string name) {
			man* cur = Root->next;
			man* pre = Root;
			for(; cur != NULL; pre=cur, cur=cur->next) {
				if(cur->name == name) {
					cout << "Find." << endl;
					cout << cur->name << " " << cur->phoneNumber << endl;
					
					pre->next = cur->next;
					delete cur;

					return SUCCESS;
				}
			}

			cout << "Contact has no member named " << name << " ." << endl;
			return FAILURE;
		}

		Stat Delete_By_Phone(string phoneNumber) {
			man* cur = Root->next;
			man* pre = Root;
			for(; cur != NULL; pre=cur, cur=cur->next) {
				if(cur->phoneNumber == phoneNumber) {
					cout << "Find." << endl;
					cout << cur->name << " " << cur->phoneNumber << endl;
					
					pre->next = cur->next;
					delete cur;

					return SUCCESS;
				}
			}

			cout << "Contact has no member phone number is " << phoneNumber << " ." << endl;
			return FAILURE;
		}

		Stat Delete() {
			if(IsEmpty() == EMPTY) {
				cout << "Sorry, Contact is Empty." << endl;
				return FAILURE;
			}
	
			cout << "1、By Name" << endl;
			cout << "2、By Phone Number" << endl;
			cout << "Please Choose: " << endl;

			string choice;
			cin >> choice;
			while(choice[0]!='1' && choice[0]!='2') {
				cout << "Error.Try Again." << endl;
				cout << "Please Choose: " << endl;
				cin >> choice;
			}

			switch(choice[0]) {
				case '1':
					InputName();
					//cout << *tname << endl;
					Delete_By_Name(tname);
					break;
				case '2':
					InputPhoneNumber();
					//string *tnumber = InputPhoneNumber();
					//cout << *tnumber << endl;
					Delete_By_Phone(tphonenumber);
					break;
			}
			
			return SUCCESS;
		}

		// 查看
		Stat Show() {
			if(IsEmpty() == EMPTY) {
				cout << "Sorry, Contact is Empty." << endl;
				return FAILURE;
			}

			man *begin = Root->next;
			// cout << begin << " " << Root << endl;
			// int n = 1;
			while(begin != NULL) {
				// cout << n++;
				cout << begin->name << " " << begin->phoneNumber << endl;
				begin = begin->next;
			}

			return SUCCESS;
		}
};

int main(void) {
	int choice;
	Contact Host;
	while(1) {
		menu();
		cin >> choice; 
		
		switch(choice) {
			case 1:
				Host.Init();
				break;
			case 2:
				Host.Insert();
				break;
			case 3:
				Host.Search();
				break;
			case 4:
				Host.Modify();
				break;
			case 5:
				Host.Delete();
				break;
			case 6:
				Host.Show();
				break;
			case 7:
				//Host.StoreAsFile();
				return 0;
				break;
			default:
				cout << "Input Error.Try again." << endl << endl;
				// system("pause");
				// system("clear");
				break;
		}
		cout << "按任意键继续。。。" << endl;
		// getchar();
		// getchar();
	}

 	
	return 0;
 }

void menu() {
	cout << "***********************" << endl;
	cout << "* 菜单内容：          *" << endl;
	cout << "* 1. 通讯录链表的建立 *" << endl;
	cout << "* 2. 通讯者信息的插入 *" << endl;
	cout << "* 3. 通讯者信息的查询 *" << endl;
	cout << "* 4. 通讯者信息的修改 *" << endl;
	cout << "* 5. 通讯者信息的删除 *" << endl;
	cout << "* 6. 通讯录链表的输出 *" << endl;
	cout << "* 7. 退出管理系统     *" << endl;
	cout << "***********************" << endl;
	cout << "*请选择：1 – 7: " << endl;
}
